home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Directory.java < prev    next >
Text File  |  1998-09-22  |  9KB  |  398 lines

  1. package com.symantec.itools.io;
  2.  
  3.  
  4. import java.io.File;
  5. import java.io.FilenameFilter;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import com.symantec.itools.util.ArrayUtils;
  11.  
  12.  
  13. /**
  14.  * @author Symantec Internet Tools Division
  15.  * @version 1.0
  16.  * @since VCafe 3.0
  17.  */
  18.  
  19. public class Directory
  20. {
  21.     /**
  22.      * @since VCafe 3.0
  23.      */
  24.     protected String name;
  25.  
  26.     /**
  27.      * @since VCafe 3.0
  28.      */
  29.     protected Directory parent;
  30.  
  31.     /**
  32.      * @since VCafe 3.0
  33.      */
  34.     protected File directory;
  35.  
  36.     protected Directory()
  37.     {
  38.     }
  39.  
  40.     public Directory(String dirName)
  41.         throws NotDirectoryException,
  42.                FileNotFoundException,
  43.                IOException
  44.     {
  45.         this(dirName, false);
  46.     }
  47.  
  48.     public Directory(String dirName, boolean create)
  49.         throws NotDirectoryException,
  50.                FileNotFoundException,
  51.                IOException
  52.     {
  53.         setName(dirName, create);
  54.     }
  55.  
  56.     /**
  57.      * @param dirName TODO
  58.      * @param create TODO
  59.      * @exception com.symantec.itools.io.NotDirectoryException
  60.      * @exception java.io.FileNotFoundException
  61.      * @exception java.io.IOException
  62.      * @since VCafe 3.0
  63.      */
  64.  
  65.     protected void setName(String dirName, boolean create)
  66.         throws NotDirectoryException,
  67.                FileNotFoundException,
  68.                IOException
  69.     {
  70.         name      = FileSystem.getCanonicalPath(dirName, true);
  71.         directory = new File(name);
  72.  
  73.         if(directory.exists())
  74.         {
  75.             if(!(directory.isDirectory()))
  76.             {
  77.                 throw new NotDirectoryException(name);
  78.             }
  79.         }
  80.         else
  81.         {
  82.             if(create)
  83.             {
  84.                 directory.mkdirs();
  85.             }
  86.             else
  87.             {
  88.                 throw new FileNotFoundException(name);
  89.             }
  90.         }
  91.     }
  92.  
  93.     /**
  94.      * @since VCafe 3.0
  95.      */
  96.  
  97.     public String getName()
  98.     {
  99.         return (name);
  100.     }
  101.  
  102.     /**
  103.      * @param recurse TODO
  104.      * @since VCafe 3.0
  105.      */
  106.  
  107.     public String[] listFiles(boolean recurse)
  108.     {
  109.         return (listFiles(new DefaultFileFilenameFilter(), recurse));
  110.     }
  111.  
  112.     /**
  113.      * @param filter TODO
  114.      * @param recurse TODO
  115.      * @since VCafe 3.0
  116.      */
  117.  
  118.     public String[] listFiles(FileFilenameFilter filter, boolean recurse)
  119.     {
  120.         String[] list;
  121.  
  122.         list = list(filter, recurse);
  123.  
  124.         if(list == null)
  125.         {
  126.             list = new String[0];
  127.         }
  128.  
  129.         return (list);
  130.     }
  131.  
  132.     /**
  133.      * @param recurse TODO
  134.      * @since VCafe 3.0
  135.      */
  136.  
  137.     public String[] listDirectories(boolean recurse)
  138.     {
  139.         return (listDirectories(new DefaultDirectoryFilenameFilter(), recurse));
  140.     }
  141.  
  142.     /**
  143.      * @param filter TODO
  144.      * @param recurse TODO
  145.      * @since VCafe 3.0
  146.      */
  147.  
  148.     public String[] listDirectories(DirectoryFilenameFilter filter, boolean recurse)
  149.     {
  150.         String[] list;
  151.  
  152.         list = list(filter, recurse);
  153.  
  154.         if(list == null)
  155.         {
  156.             list = new String[0];
  157.         }
  158.  
  159.         return (list);
  160.     }
  161.  
  162.     /**
  163.      * @param recurse TODO
  164.      * @since VCafe 3.0
  165.      */
  166.  
  167.     public String[] list(boolean recurse)
  168.     {
  169.         return (list((FilenameFilter)null, recurse));
  170.     }
  171.  
  172.     /**
  173.      * @param filter TODO
  174.      * @param recurse TODO
  175.      * @since VCafe 3.0
  176.      */
  177.  
  178.     public String[] list(FilenameFilter filter, boolean recurse)
  179.     {
  180.         String[] list;
  181.  
  182.         if(directory == null)
  183.         {
  184.             return (null);
  185.         }
  186.  
  187.         list = directory.list(filter);
  188.  
  189.         for(int i = 0; i < list.length; i++)
  190.         {
  191.             list[i] = (new File(name + list[i])).getAbsolutePath();
  192.         }
  193.  
  194.         if(recurse)
  195.         {
  196.             String[] directories;
  197.  
  198.             directories = listDirectories(false);
  199.  
  200.             for(int i = 0; i < directories.length; i++)
  201.             {
  202.                 try
  203.                 {
  204.                     String[] dirs;
  205.  
  206.                     dirs = new Directory(directories[i]).list(filter, true);
  207.                     list = (String[])ArrayUtils.append(new String[list.length + dirs.length], list, dirs);
  208.                 }
  209.                 catch(IOException ex)
  210.                 {
  211.                     ex.printStackTrace();
  212.                 }
  213.             }
  214.         }
  215.  
  216.         return (list);
  217.     }
  218.  
  219.     /**
  220.      * @param filter TODO
  221.      * @param dst TODO
  222.      * @param recurse TODO
  223.      * @since VCafe 3.0
  224.      */
  225.  
  226.     public boolean copyTo(Directory dst, boolean recurse)
  227.         throws IOException
  228.     {
  229.         return (copyTo(new DefaultFileFilenameFilter(), dst, recurse));
  230.     }
  231.  
  232.     public boolean copyTo(FilenameFilter filter, Directory dst, boolean recurse)
  233.         throws IOException
  234.     {
  235.         String   targetPathName;
  236.         String   sourceRootPathName;
  237.         String[] inputListing;
  238.  
  239.         if(getName().equals(dst.getName()))
  240.         {
  241.             return (false);
  242.         }
  243.  
  244.         targetPathName     = FileSystem.getCanonicalPath(dst.getName(), true);
  245.         sourceRootPathName = FileSystem.getCanonicalPath(getName(), true);
  246.  
  247.         inputListing = list(filter, recurse);
  248.  
  249.         for(int i = 0; i < inputListing.length; i++)
  250.         {
  251.             String subdir;
  252.             File   sourceFile;
  253.             String sourcePath;
  254.             File   targetFile;
  255.  
  256.             subdir     = "";
  257.             sourceFile = new File(inputListing[i]);
  258.             sourcePath = FileSystem.getCanonicalPath(sourceFile.getParent(), true);
  259.  
  260.             // create new subdirectory if necessary
  261.             if(!(sourceRootPathName.equals(sourcePath)))
  262.             {
  263.                 subdir = sourcePath.substring(sourceRootPathName.length(), sourcePath.length());
  264.                 (new File(targetPathName + subdir)).mkdirs();
  265.             }
  266.  
  267.             targetFile = new File(FileSystem.getCanonicalPath(targetPathName + subdir, true) + sourceFile.getName());
  268.  
  269.             if(!sourceFile.isDirectory())
  270.             {
  271.                 if(!(copyFile(sourceFile, targetFile)))
  272.                 {
  273.                     return (false);
  274.                 }
  275.             }
  276.             else
  277.             {
  278.                 System.out.println("** not copying directory " + sourceFile);
  279.             }
  280.         }
  281.  
  282.         return true;
  283.     }
  284.  
  285.     /**
  286.      * @param source TODO
  287.      * @param target TODO
  288.      * @exception java.io.IOException
  289.      * @since VCafe 3.0
  290.      */
  291.     protected boolean copyFile(File source, File target)
  292.         throws IOException
  293.     {
  294.         return (FileSystem.copyFile(source, target));
  295.     }
  296.  
  297.     /**
  298.      * @param filter TODO
  299.      * @param dst TODO
  300.      * @param recurse TODO
  301.      * @since VCafe 3.0
  302.      */
  303.  
  304.     public boolean moveTo(FilenameFilter filter, Directory dst, boolean recurse)
  305.     {
  306.         throw new com.symantec.itools.lang.NotImplementedError(com.symantec.itools.lang.Debug.getExecutionContext(2));
  307.     }
  308.  
  309.     /**
  310.      * @since VCafe 3.0
  311.      */
  312.  
  313.     public boolean delete()
  314.     {
  315.         return (directory.delete());
  316.     }
  317.  
  318.     public boolean deleteAll()
  319.     {
  320.         String[] list;
  321.  
  322.         list = listFiles(true);
  323.  
  324.         for(int i = 0; i < list.length; i++)
  325.         {
  326.             if(!(new File(list[i]).delete()))
  327.             {
  328.                 return (false);
  329.             }
  330.         }
  331.  
  332.         list = listDirectories(true);
  333.  
  334.         for(int i = list.length - 1; i >= 0; i--)
  335.         {
  336.             if(!(new File(list[i]).delete()))
  337.             {
  338.                 return (false);
  339.             }
  340.         }
  341.  
  342.         return (true);
  343.     }
  344.  
  345.     /**
  346.      * @since VCafe 3.0
  347.      */
  348.  
  349.     public boolean canRead()
  350.     {
  351.         return (directory.canRead());
  352.     }
  353.  
  354.     /**
  355.      * @since VCafe 3.0
  356.      */
  357.  
  358.     public boolean canWrite()
  359.     {
  360.         return (directory.canWrite());
  361.     }
  362.  
  363.     /**
  364.      * @since VCafe 3.0
  365.      */
  366.  
  367.     public String getParent()
  368.     {
  369.         return (directory.getParent());
  370.     }
  371.  
  372.     /**
  373.      * @since VCafe 3.0
  374.      */
  375.  
  376.     public long lastModified()
  377.     {
  378.         return (directory.lastModified());
  379.     }
  380.  
  381.     /**
  382.      * @param dir TODO
  383.      * @since VCafe 3.0
  384.      */
  385.  
  386.     public boolean renameTo(AbstractDirectory dir)
  387.     {
  388.         throw new com.symantec.itools.lang.NotImplementedError(com.symantec.itools.lang.Debug.getExecutionContext(2));
  389.     }
  390.  
  391.     /**
  392.      * @since VCafe 3.0
  393.      */
  394.     public boolean exists()
  395.     {
  396.         return (directory.exists());
  397.     }
  398. }